package indexer;
import java.util.*;

public class IndexTerm {
	private Integer numOfFile;
	private Vector<Integer> fileSN;
	private Vector<TermDocument> documents;
	private Integer lastFile;
	
	public IndexTerm (){
		numOfFile=new Integer(0);
		fileSN=new Vector<Integer>();
		documents=new Vector<TermDocument>();
		lastFile=new Integer(0);
	}
	
	public void adddoc(int fSN){
		if (fileSN.contains(fSN)){
			System.out.println("Warning Message from IndexTerm: " +
					"the fSN to add already exists");
			return;
		}
		fileSN.add(fSN);
		lastFile=numOfFile++;
		documents.add(new TermDocument());
			
	}
	
	public void addpos(int fSN,int pos){
		if (fileSN.contains(fSN)){
			lastFile=fileSN.indexOf(fSN);
		}
		else{
			lastFile=numOfFile++;
			fileSN.add(fSN);
			documents.add(new TermDocument());			
		}
		documents.elementAt(lastFile).add(pos);
	}
	
	public void addpos(int pos){
		documents.elementAt(lastFile).add(pos);
	}
	
	public void addpos(Vector<Integer> vecpos){
		documents.elementAt(lastFile).add(vecpos);
	}
	
	public void deletepos(int fSN,int pos){
		if (!fileSN.contains(fSN)){
			System.out.println("Warning Message from IndexTerm: "
					+ "the fSN for pos deletion does not exist");
			return;
		}
		lastFile=documents.indexOf(fSN);
		documents.elementAt(lastFile).delete(pos);
	}
	
	public void deletepos(int pos){
		documents.elementAt(lastFile).delete(pos);
	}
	
	public void deletedoc(int fSN){
		if (!fileSN.contains(fSN)){
			System.out.println("Warning Message from IndexTerm: "
					+ "the fSN to delete does not exist");
			return;
		}
		int delpos=fileSN.indexOf(fSN);
		fileSN.removeElementAt(delpos);
		documents.removeElementAt(delpos);
		numOfFile--;
	}
	
	public void sortdoc(int fSN){
		if (!fileSN.contains(fSN)){
			System.out.println("Warning Message from IndexTerm: "
					+ "the fSN to sort does not exist");
			return;
		}
		lastFile=fileSN.indexOf(fSN);
		documents.elementAt(lastFile).sort();
	}
	
	public void sortdoc(){
		documents.elementAt(lastFile).sort();
	}
	
	public boolean isdocsorted (int fSN){
		if (!fileSN.contains(fSN)){
			System.out.println("Warning Message from IndexTerm: "
					+ "the fSN to query sorted does not exist");
			return false;
		}
		lastFile=fileSN.indexOf(fSN);
		return documents.elementAt(lastFile).issorted();
	}
	
	public boolean isdocsorted(){
		return documents.elementAt(lastFile).issorted();
	}
	
	public Integer returnNumberOfFile (){
		return numOfFile;
	}
	
	public Vector<Integer> returnfileSN(){
		Vector<Integer> ffSN=new Vector<Integer>(fileSN.size());
		Enumeration<Integer> en=fileSN.elements();
		int temp;
		do{
			temp=en.nextElement();
			ffSN.add(temp);
		}while(en.hasMoreElements());
		return ffSN;
	}
	
	public Vector<Integer> returnpos(int fSN){
		if (!fileSN.contains(fSN)){
			System.out.println("Warning Message from IndexTerm: "
					+ "the fSN to return pos does not exist");
			return null;
		}
		lastFile=fileSN.indexOf(fSN);
		return documents.elementAt(lastFile).returnpos();
	}
	
	public Vector<Integer> returnpos(){
		return documents.elementAt(lastFile).returnpos();
	}
	
	public void setCurrentFile(int fSN){
		if (!fileSN.contains(fSN)){
			System.out.println("Warning Message from IndexTerm: "
					+ "the fSN to set current file does not exist");
			return;
		}
		lastFile=fileSN.indexOf(fSN);
	}
	
	public boolean containsFile(int fSN){
		return fileSN.contains(fSN);
	}
}
